<!--
  * new page
  * @author: liuxin
  * @since: 2024-08-25
  * index.vue
 -->
<template>
    <breadCrumb ref="breadcrumb" :item="item"></breadCrumb>
    <div class="table-wrapped">
        <div class="table-top">
            <div class="table-header">
                <div>
                    <el-input v-model="name" class="w-50 m-2" size="large" placeholder="输入帐号进行搜索" :prefix-icon="Search"
                        @change="searchLoginLogFn()" clearable @clear="getloginFirstPageList" />
                </div>
                <div class="upload-wrapped">

                    <el-button type="danger" @click="clearList">清空登录日志</el-button>

                </div>
            </div>
            <div class="table-content">
                <el-table :data="tableData" style="width: 100%;" border stripe>
                    <el-table-column type="index" width="50"></el-table-column>
                    <el-table-column prop="account" label="账号"></el-table-column>
                    <el-table-column prop="name" label="名字"> </el-table-column>
                    <el-table-column prop="email" label="邮箱/联系方式"></el-table-column>
                    <el-table-column prop="login_time" label="登录时间">
                        <template #default="{ row }">
                            <div>{{ row.login_time?.slice(0, 16) }}</div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div class="table-footer">
            <el-pagination :page-size="1" :current-page="paginationData.loginCurrentPage" :pager-count="7"
                :page-count="paginationData.loginPageCount" :total="paginationData.loginTotal"
                @current-change="loginCurrentChange" layout="prev, pager, next" />
        </div>
    </div>
    <tips ref="tip" @success="getloginFirstPageList"></tips>
</template>

<script setup lang="ts">
import { ref, reactive, onBeforeUnmount } from "vue"
import breadCrumb from '@/components/bread_crumb.vue'
import { Search } from '@element-plus/icons-vue';
import { searchloginLogList, loginLogListLength, returnLoginListData } from '@/api/log'
import tips from "./components/tips.vue"

//面包屑
const breadcrumb = ref()
//面包屑参数
const item = ref({
    first: '登录日志',
    second: '',
})

// 文件表格数据
const tableData = ref([])

//  分页数据
const paginationData = reactive({
    // 文件名总数
    loginTotal: 1,
    // 文件名列表总页数
    loginPageCount: 1,
    // 文件名列表当前所处页数
    loginCurrentPage: 1,

})
// 获取文件列表的页数
const getloginLengthFn = async () => {
    const res = await loginLogListLength()
    paginationData.loginTotal = res.length
    paginationData.loginPageCount = Math.ceil(res.length / 10)
}
getloginLengthFn()
// 默认获取文件列表第一页的数据
const getloginFirstPageList = async () => {
    tableData.value = await returnLoginListData(1)
}
getloginFirstPageList()

// 文件列表监听换页
const loginCurrentChange = async (value: number) => {
    paginationData.loginCurrentPage = value
    tableData.value = await returnLoginListData(paginationData.loginCurrentPage)
}

const name = ref()
const searchLoginLogFn = async () => {
    tableData.value = await searchloginLogList(name.value)
}

const tip = ref()
const clearList = () => {
    tip.value.open()
}
/* // 取消订阅/监听
onBeforeUnmount(() => {
    bus.all.clear()
}) */
</script>

<style scoped lang="scss">
:deep(.el-table .cell) {
    font-weight: 400;
}

a {
    color: #fff;
    display: block;
    text-decoration: none;
}
</style>